from ..containers.frame import TgFrame
from ..buttons.nav_button import NavButton
from functools import partial

class TgBottomNavBar(TgFrame):
    def __init__(self, parent=None, height='medium'):
        super().__init__(
            parent=parent,
            layout_type='hbox',
            layout_margins="small",
            layout_spacing="medium"
        )

        self.setObjectName("bottom_nav_frame")
        self.setStyleSheet("#bottom_nav_frame { border-top: 1px solid #e0e0e0; }")
        self._buttons = []
        self._height = height

    def add_item(self, text, icon=None, on_click=None):
        button = NavButton(text, size=self._height)
        if on_click:
            button.clicked.connect(on_click)
        
        button.clicked.connect(partial(self.set_active_button, button))
        self._buttons.append(button)
        self.addWidget(button)

    def add_items(self, items):
        for item in items:
            text = item.get("text", "")
            icon = item.get("icon")
            on_click = item.get("on_click")
            self.add_item(text, icon, on_click)
        
        if self._buttons:
            self.set_active_button(self._buttons[0])

    def set_active_button(self, active_button):
        for button in self._buttons:
            button.set_active(button == active_button)